home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / codecs / qdmediamaker / application files / comapplication.c next >
Encoding:
Text File  |  2000-06-23  |  10.9 KB  |  445 lines

  1. //////////
  2. //
  3. //    File:        ComApplication.c
  4. //
  5. //    Contains:    Application-specific code for basic QuickTime movie display and control.
  6. //                This file is used for BOTH MacOS and Windows.
  7. //
  8. //    Written by:    Tim Monroe
  9. //                Based (heavily!) on the MovieShell code written by Apple DTS.
  10. //
  11. //    Copyright:    © 1994-1998 by Apple Computer, Inc., all rights reserved.
  12. //
  13. //    Change History (most recent first):
  14. //
  15. //       <12>         02/01/99    rtm        reworked window title handling to remove "\p" sequences
  16. //       <11>         12/16/98    rtm        removed all QTVR API calls, so we can remove QTVR.lib from project
  17. //       <10>         10/23/97    rtm        moved InitializeQTVR to InitApplication, TerminateQTVR to StopApplication
  18. //       <9>         10/13/97    rtm        reworked HandleApplicationMenu to use menu identifiers
  19. //       <8>         09/11/97    rtm        merged MacApplication.c and WinApplication.c into ComApplication.c
  20. //       <7>         08/21/97    rtm        first file for Windows; based on MacApplication.c for Mac sample code
  21. //       <6>         06/04/97    rtm        removed call to QTVRUtils_IsQTVRMovie in InitApplicationWindowObject
  22. //       <5>         02/06/97    rtm        fixed window resizing code
  23. //       <4>         12/05/96    rtm        added hooks into MacFramework.c: StopApplication, InitApplicationWindowObject
  24. //       <3>         12/02/96    rtm        added cursor updating to DoIdle
  25. //       <2>         11/27/96    rtm        conversion to personal coding style; added preliminary QTVR support
  26. //       <1>         12/21/94    khs        first file
  27. //       
  28. //
  29. //    QTShell is a simple QuickTime viewer framework. It demonstrates how to incorporate 
  30. //    QuickTime and QuickTime VR movies into an application. QTShell is based heavily on the 
  31. //    MovieShell QuickTime playback framework included with the DTS QT sample code.
  32. //
  33. //////////
  34.  
  35. //////////
  36. //
  37. // header files
  38. //
  39. //////////
  40.  
  41. #include "ComApplication.h"
  42. #include "QDMMaker.h"
  43. #include "QDrawHandler.h"
  44.   
  45. // global variables for Macintosh code
  46. #if TARGET_OS_MAC
  47. #endif
  48.  
  49. // global variables for Windows code
  50. #if TARGET_OS_WIN32
  51. extern HWND                ghWnd;                                    // the MDI frame window; this window has the menu bar
  52. extern int                gNumWindowsOpen;
  53. extern LPSTR            gCmdLine;
  54. #endif
  55.  
  56. long                    gMaxMilliSecToUse = 0L;
  57.  
  58. extern WindowPtr        gWindow;
  59.  
  60. Component                gComponent = NULL;
  61.  
  62.  
  63. //////////
  64. //
  65. // InitApplication
  66. // Do any application-specific initialization.
  67. //
  68. // The theStartPhase parameter determines which "phase" of application start-up is executed,
  69. // *before* the MDI frame window is created or *after*. This distinction is relevant only on
  70. // Windows, so on MacOS, you should always use kInitAppPhase_BothPhases.
  71. //
  72. //////////
  73.  
  74. void InitApplication (UInt32 theStartPhase)
  75. {
  76.     Rect            myRect;
  77.     StringPtr        myTitle = QTUtils_ConvertCToPascalString(kWindowTitle);
  78.     
  79.     // ***do any start-up activities that should occur before the MDI frame window is created
  80.     if (theStartPhase & kInitAppPhase_BeforeCreateFrameWindow) {
  81.  
  82.     }    // end of kInitAppPhase_BeforeCreateFrameWindow
  83.  
  84.     // ***do any start-up activities that should occur after the MDI frame window is created
  85.     if (theStartPhase & kInitAppPhase_AfterCreateFrameWindow) {
  86. #if TARGET_OS_WIN32
  87.         // on Windows, open as movie documents any files specified on the command line
  88.         SendMessage(ghWnd, WM_OPENDROPPEDFILES, 0L, 0L);
  89. #endif
  90.  
  91.         // create output window
  92.         MacSetRect(&myRect, 10, 40, 510, 440);
  93.         gWindow = NewCWindow(NULL, &myRect, myTitle, true, documentProc, (WindowPtr)-1, false, 0);
  94.         MacSetPort(gWindow);
  95.         
  96.         free(myTitle);
  97.     }    // end of kInitAppPhase_AfterCreateFrameWindow
  98. }
  99.  
  100.  
  101. //////////
  102. //
  103. // StopApplication
  104. // Do any application-specific shut-down.
  105. //
  106. // The theStopPhase parameter determines which "phase" of application shut-down is executed,
  107. // *before* any open movie windows are destroyed or *after*.
  108. //
  109. //////////
  110.  
  111. void StopApplication (UInt32 theStopPhase)
  112. {
  113.     if (gWindow != NULL) {
  114.         DisposeWindow(gWindow);
  115.         gWindow = NULL;
  116.     }    
  117.     
  118.     // do any shut-down activities that should occur after the movie windows are destroyed
  119.     if (theStopPhase & kStopAppPhase_AfterDestroyWindows) {
  120.         
  121.     }    // end of kStopAppPhase_AfterDestroyWindows
  122. }
  123.  
  124.  
  125. //////////
  126. //
  127. // DoIdle
  128. // Do any processing that can/should occur at idle time.
  129. //
  130. //////////
  131.  
  132. void DoIdle (WindowReference theWindow)
  133. {
  134.     WindowObject         myWindowObject = NULL;
  135.     GrafPtr             mySavedPort;
  136.     
  137.     GetPort(&mySavedPort);
  138.     MacSetPort(GetPortFromWindowReference(theWindow));
  139.     
  140.     myWindowObject = GetWindowObjectFromWindow(theWindow);
  141.     if (myWindowObject != NULL) {
  142.         MovieController        myMC = NULL;
  143.     
  144.         myMC = (**myWindowObject).fController;
  145.         if (myMC != NULL) {
  146.  
  147. #if TARGET_OS_MAC
  148.             // restore the cursor to the arrow
  149.             // if it's outside the front movie window or outside the window's visible region
  150.             if (theWindow == GetFrontMovieWindow()) {
  151.                 Rect    myRect;
  152.                 Point    myPoint;
  153.                 
  154.                 GetMouse(&myPoint);
  155.                 MCGetControllerBoundsRect(myMC, &myRect);
  156.                 if (!MacPtInRect(myPoint, &myRect) || !PtInRgn(myPoint, GetPortFromWindowReference(theWindow)->visRgn))
  157.                     MacSetCursor(&qd.arrow);
  158.             }
  159. #endif    // TARGET_OS_MAC
  160.         }
  161.     }
  162.     
  163.     // @@@INSERT APPLICATION-SPECIFIC IDLE-TIME FUNCTIONALITY HERE
  164.     
  165.     MacSetPort(mySavedPort);
  166. }
  167.  
  168.  
  169. //////////
  170. //
  171. // DoUpdateWindow
  172. // Update the specified window.
  173. //
  174. //////////
  175.  
  176. void DoUpdateWindow (WindowReference theWindow, Rect *theRefreshArea)
  177. {
  178. #pragma unused(theRefreshArea)
  179.  
  180.     GrafPtr             mySavedPort;
  181.     
  182.     GetPort(&mySavedPort);
  183.     MacSetPort(GetPortFromWindowReference(theWindow));
  184.     
  185.     BeginUpdate(GetPortFromWindowReference(theWindow));
  186.     //EraseRect(theRefreshArea);        // this is important, for non-rectangular movies
  187.     
  188.     // @@@INSERT APPLICATION-SPECIFIC DRAWING FUNCTIONALITY HERE
  189.     
  190.     // draw the movie controller and its movie
  191.     MCDoAction(GetMCFromWindow(theWindow), mcActionDraw, theWindow);
  192.     
  193.     EndUpdate(GetPortFromWindowReference(theWindow));
  194.     MacSetPort(mySavedPort);
  195. }
  196.  
  197.  
  198. //////////
  199. //
  200. // HandleContentClick
  201. // Handle mouse button clicks in the specified window.
  202. //
  203. //////////
  204.  
  205. void HandleContentClick (WindowReference theWindow, EventRecord *theEvent)
  206. {
  207. #pragma unused(theEvent)
  208.  
  209.     GrafPtr             mySavedPort;
  210.     
  211.     GetPort(&mySavedPort);
  212.     MacSetPort(GetPortFromWindowReference(theWindow));
  213.     
  214.     // @@@INSERT APPLICATION-SPECIFIC CONTENT CLICKING FUNCTIONALITY HERE
  215.  
  216.     MacSetPort(mySavedPort);
  217. }
  218.  
  219.  
  220. //////////
  221. //
  222. // HandleApplicationKeyPress
  223. // Handle application-specific key presses.
  224. // Returns true if the key press was handled, false otherwise.
  225. //
  226. //////////
  227.  
  228. Boolean HandleApplicationKeyPress (char theCharCode)
  229. {
  230.     Boolean        isHandled = true;
  231.     
  232.     switch (theCharCode) {
  233.     
  234.         // @@@HANDLE APPLICATION-SPECIFIC KEY PRESSES HERE
  235.  
  236.         default:
  237.             isHandled = false;
  238.             break;
  239.     }
  240.  
  241.     return(isHandled);
  242. }
  243.  
  244.  
  245. #if TARGET_OS_MAC
  246. //////////
  247. //
  248. // CreateMovieWindow
  249. // Create a window to display a movie in.
  250. //
  251. //////////
  252.  
  253. WindowRef CreateMovieWindow (Rect *theRect, Str255 theTitle)
  254. {
  255.     WindowRef            myWindow;
  256.     
  257.     myWindow = NewCWindow(NULL, theRect, theTitle, false, noGrowDocProc, (WindowPtr)-1L, true, 0);
  258.     return(myWindow);
  259. }
  260. #endif
  261.  
  262.  
  263. //////////
  264. //
  265. // HandleApplicationMenu
  266. // Handle selections in the application's menus.
  267. //
  268. // The theMenuItem parameter is a UInt16 version of the Windows "menu item identifier". 
  269. // When called from Windows, theMenuItem is simply the menu item identifier passed to the window proc.
  270. // When called from MacOS, theMenuItem is constructed like this:
  271. //     *high-order 8 bits == the Macintosh menu ID (1 thru 256)
  272. //     *low-order 8 bits == the Macintosh menu item (sequential from 1 to ordinal of last menu item in menu)
  273. // In this way, we can simplify the menu-handling code. There are, however, some limitations,
  274. // mainly that the menu item identifiers on Windows must be derived from the Mac values. 
  275. //
  276. //////////
  277.  
  278. void HandleApplicationMenu (UInt16 theMenuItem)
  279. {
  280.     switch (theMenuItem) {
  281.         case IDM_ROWS:
  282.             QDMM_MakeQDMovie(theMenuItem, 500, 400);
  283.             break;
  284.         case IDM_LINES:
  285.             QDMM_MakeQDMovie(theMenuItem, 256, 200);
  286.             break;
  287.         case IDM_BOXES:
  288.             QDMM_MakeQDMovie(theMenuItem, 300, 200);
  289.             break;
  290.         case IDM_BALL:
  291.             QDMM_MakeQDMovie(theMenuItem, 500, 400);
  292.             break;
  293.  
  294.         default:
  295.             break;
  296.     }    // switch (theMenuItem)
  297. }
  298.  
  299.  
  300. //////////
  301. //
  302. // AdjustApplicationMenus
  303. // Adjust state of items in the application's menus.
  304. //
  305. // Currently, the Mac application has only one app-specific menu ("Test"); you could change that.
  306. //
  307. //////////
  308.  
  309. void AdjustApplicationMenus (WindowReference theWindow, MenuReference theMenu)
  310. {
  311. #pragma unused(theWindow, theMenu)
  312.  
  313. #if TARGET_OS_MAC
  314.     if (FrontWindow() == gWindow)
  315.         SetMenuItemState(GetMenuHandle(mFile), iClose, kDisableMenuItem);
  316.  
  317.     // we don't allow creating new files here...
  318.     SetMenuItemState(GetMenuHandle(mFile), iNew, kDisableMenuItem);
  319.     
  320.     // we don't allow opening files here...
  321.     SetMenuItemState(GetMenuHandle(mFile), iOpen, kDisableMenuItem);
  322. #endif
  323.  
  324. }
  325.  
  326.  
  327. //////////
  328. //
  329. // DoApplicationEventLoopAction
  330. // Perform any application-specific event loop actions.
  331. //
  332. // Return true to indicate that we've completely handled the event here, false otherwise.
  333. //
  334. //////////
  335.  
  336. Boolean DoApplicationEventLoopAction (EventRecord *theEvent)
  337. {
  338. #pragma unused(theEvent)
  339.     
  340.     return(false);            // no-op for now
  341. }
  342.  
  343.  
  344. //////////
  345. //
  346. // AddControllerFunctionality
  347. // Configure the movie controller.
  348. //
  349. //////////
  350.  
  351. void AddControllerFunctionality (MovieController theMC)
  352. {
  353.     long            myControllerFlags;
  354.     
  355.     // CLUT table use
  356.     MCDoAction(theMC, mcActionGetFlags, &myControllerFlags);
  357.     MCDoAction(theMC, mcActionSetFlags, (void *)(myControllerFlags | mcFlagsUseWindowPalette));
  358.  
  359.     // enable keyboard event handling
  360.     MCDoAction(theMC, mcActionSetKeysEnabled, (void *)true);
  361.     
  362.     // disable drag support
  363.     MCDoAction(theMC, mcActionSetDragEnabled, (void *)false);
  364. }
  365.  
  366.  
  367. //////////
  368. //
  369. // InitApplicationWindowObject
  370. // Do any application-specific initialization of the window object.
  371. //
  372. //////////
  373.  
  374. void InitApplicationWindowObject (WindowObject theWindowObject)
  375. {
  376. #pragma unused(theWindowObject)
  377.  
  378.     // @@@INSERT APPLICATION-SPECIFIC WINDOW OBJECT START-UP HERE
  379. }
  380.  
  381.  
  382. //////////
  383. //
  384. // RemoveApplicationWindowObject
  385. // Do any application-specific clean-up of the window object.
  386. //
  387. //////////
  388.  
  389. void RemoveApplicationWindowObject (WindowObject theWindowObject)
  390. {
  391. #pragma unused(theWindowObject)
  392.     
  393.     // @@@INSERT APPLICATION-SPECIFIC WINDOW OBJECT CLEAN-UP HERE
  394.  
  395.     // DoDestroyMovieWindow in MacFramework.c or MovieWndProc in WinFramework.c
  396.     // releases the window object itself
  397. }
  398.  
  399.  
  400. //////////
  401. //
  402. // ApplicationMCActionFilterProc 
  403. // Intercept some mc actions for the movie controller.
  404. //
  405. // NOTE: The theRefCon parameter is a handle to a window object record.
  406. //
  407. //////////
  408.  
  409. PASCAL_RTN Boolean ApplicationMCActionFilterProc (MovieController theMC, short theAction, void *theParams, long theRefCon)
  410. {
  411. #pragma unused(theMC, theParams)
  412.  
  413.     Boolean                isHandled = false;            // false => allow controller to process the action
  414.     WindowObject        myWindowObject = NULL;
  415.     
  416.     myWindowObject = (WindowObject)theRefCon;
  417.     if (myWindowObject == NULL)
  418.         return(isHandled);
  419.         
  420.     switch (theAction) {
  421.     
  422.         // handle window resizing
  423.         case mcActionControllerSizeChanged:
  424.             SizeWindowToMovie(myWindowObject);
  425.             break;
  426.  
  427.         // handle idle events
  428.         case mcActionIdle:
  429.             DoIdle((**myWindowObject).fWindow);
  430.             break;
  431.             
  432.         // handle URL events
  433.         case mcActionLinkToURL:
  434.             break;
  435.             
  436.         default:
  437.             break;
  438.             
  439.     }    // switch (theAction)
  440.     
  441.     return(isHandled);    
  442. }
  443.  
  444.  
  445.